﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HMIS.FrameWork.Repository.Nhb;
using HMIS.Infrastructure.Core;
using HMIS.Infrastructure.Core.Repositories;
using HMIS.FrameWork.DomainBase;

namespace HMIS.Infrastructure.Core.Repositories.Impl
{
    public class DicCategoryRepositoryImpl : RepositoryNhbImpl<DicCategory>,IDicCategoryRepository
    {
		public IList<DicCategory> QueryPlist(string query, int start, int limit, out long total)
		{
			var q = Session.CreateQuery(@"select d from DicCategory as d
                                                    where lower(d.Name) like lower(:query)
                                                    or lower(d.TreeCode) like lower(:query)
                                                ")
						.SetString("query", "%" + query + "%")
						.SetFirstResult(start)
						.SetMaxResults(limit);

			total = Session.CreateQuery(@"select count(*) from DicCategory as d
                                                    where lower(d.Name) like lower(:query)
                                                    or lower(d.TreeCode) like lower(:query)
                                                    ")
				   .SetString("query", "%" + query + "%")
				   .UniqueResult<long>();

			return q.List<DicCategory>();
		}

		public IList<DicCategory> GetPlistAll(int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			var strFilter = base.GetHqlstrByExtFilter(filters, "r");
			sort = "r." + sort;

			var query = Session.CreateQuery(@"select r from DicCategory as r"
					+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter)
					+ " order by " + sort + " " + dir)
					.SetFirstResult(start)
					.SetMaxResults(limit);

			total = Session.CreateQuery(@"select count(*) from DicCategory as r"
				 + (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter))
				.UniqueResult<long>();

			return query.List<DicCategory>();
		}

		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}
    }
}
